/*
 * @Author: your name
 * @Date: 2021-11-18 08:57:34
 * @LastEditTime: 2022-12-20 16:41:45
 * @LastEditors: tushan
 * @Description: 路由配置表
 * @FilePath: /mcecyClient/src/util/Router.js
 */
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import ajax from "./ajax";
import cache from "./cache";
import sm from "./sm2";
const routes = [
    {
        path: "/",
        name: "index",
        component: () => import("../views/index/index.vue"),
        meta: { title: "快一点导航起始页" }
    },
    {
        path: "/Manager",
        name: "Manager",
        component: () => import("../views/Manager/layout.vue"),
        meta: { title: "后台管理", permission: true },
        redirect: '/Manager/index',
        children: [{
            path: "index",
            name: "ManagerIndex",
            component: () => import("../views/Manager/index.vue"),
            meta: { title: "数据统计", permission: true },
        },
        {
            path: "user",
            name: "ManagerUser",
            component: () => import("../views/Manager/user.vue"),
            meta: { title: "用户管理", permission: true },
        },
        {
            path: "setting",
            name: "ManagerSetting",
            component: () => import("../views/Manager/setting.vue"),
            meta: { title: "系统设置", permission: true }
        },
        {
            path: 'CdnRefresh',
            name: 'CdnRefresh',
            component: () => import("../views/Manager/CdnRefresh.vue"),
            meta: { title: "CDN刷新", permission: true }
        },
        {
            path: 'linkstore',
            name: 'Linkstore',
            component: () => import("../views/Manager/linkstore.vue"),
            meta: { title: "软件商城", permission: true }
        }]
    }
]
    ;
const router = createRouter({
    history: createWebHistory(),
    routes,
});
/*路由守卫*/
router.beforeEach((to, from, next) => {
    if (to['meta']['permission']) {
        if (cache.get("token")) {
            ajax({
                url: '/user/get'
            }).then(el => {
                let dt = JSON.parse(sm.decode(el.data))
                if (dt.manager == 1) {
                    next();
                } else {
                    next("/")
                }
            }).catch(() => { next("/") })
        } else {
            next("/")
        }
    } else {
        next()
    }


});
export default router;
